package com.example.demovote.controller;


import com.example.demovote.entity.User;
import com.example.demovote.entity.Vote;
import com.example.demovote.mapper.VoteMapper;
import com.example.demovote.service.IVoteService;
import com.example.demovote.service.impl.VoteServiceImpl;
import com.example.demovote.vo.Result;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 投票系统-题目表-2 前端控制器
 * </p>
 *
 * @author ly
 * @since 2024-03-18
 */
@RestController
@RequestMapping("/demovote/vote")
public class VoteController {

    @Resource
    VoteMapper voteMapper;

    @Resource
    IVoteService voteService;

    @RequestMapping("query")
    public List<Vote> query(String vname){
        return voteMapper.selectWithCnt(vname);
    }

    @RequestMapping("create")
    public Result create(String vname, String vtype, String[] iname){
        return voteService.create(vname,vtype,iname);
    }

    @RequestMapping("find")
    public Vote find(int id){
        return voteMapper.selectById(id);
    }

    @RequestMapping("commit")
    public Result commit(int vid, int[] iid,
                         @SessionAttribute User loginedUser){
        int uid = loginedUser.getId();
        return voteService.commit(vid,uid,iid);
    }
}

